package dyyx.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dyyx.dto.WebResult;

public abstract class CodecWebUtil {

	private static final Set<String> OPS = new HashSet<>();

	private static final String OP_BYTE = "byte";
	private static final String OP_BIT = "bit";


	private static final String OP_MD5 = "md5";
	private static final String OP_SHA256 = "sha256";
	private static final String OP_SHA384 = "sha384";
	private static final String OP_SHA512 = "sha512";

	private static final String OP_URLENCODE = "urlencode";
	private static final String OP_URLDECODE = "urldecode";


	private static final String OP_BASE64ENCODE = "base64encode";
	private static final String OP_BASE64DECODE = "base64decode";

	

	private static final String OP_BASE64URLENCODE = "base64urlencode";
	private static final String OP_BASE64URLDECODE = "base64urldecode";
	
	private static final String OP_unicodeencode = "unicodeencode";
	private static final String OP_unicodedecode = "unicodedecode";


	static {
		
		OPS.add(OP_BYTE);
		OPS.add(OP_BIT);


		OPS.add(OP_MD5);
		OPS.add(OP_SHA256);
		OPS.add(OP_SHA384);
		OPS.add(OP_SHA512);
		
		OPS.add(OP_URLENCODE);
		OPS.add(OP_URLDECODE);

		OPS.add(OP_BASE64ENCODE);
		OPS.add(OP_BASE64DECODE);
		OPS.add(OP_BASE64URLENCODE);
		OPS.add(OP_BASE64URLDECODE);
		
		OPS.add(OP_unicodeencode);
		OPS.add(OP_unicodedecode);


	}

	public static String byteToBit(byte b) {		
		StringBuilder sb = new StringBuilder(8);
		sb.append((byte) ((b >> 7) & 0x1));
		sb.append((byte) ((b >> 6) & 0x1));
		sb.append((byte) ((b >> 5) & 0x1));
		sb.append((byte) ((b >> 4) & 0x1));
		sb.append((byte) ((b >> 3) & 0x1));
		sb.append((byte) ((b >> 2) & 0x1));
		sb.append((byte) ((b >> 1) & 0x1));
		sb.append((byte) ((b >> 0) & 0x1));
		return sb.toString();
	}
	

	public static void run(HttpServletRequest request, WebResult webResult) throws Exception {
		try {
			run_(request, webResult);
		} catch (Throwable e) {
			webResult.msg = webResult.op + " error," + e;
		}
	}
	
	

	private static void run_(HttpServletRequest request, WebResult webResult) throws Exception {
		String op = WebUtil.get(request, "op");
		String content = WebUtil.get(request, "content");
		String charset = WebUtil.get(request, "charset");


		if (StringUtils.isBlank(op)) {
			webResult.msg = "op blank";
			return;
		}
		if (!OPS.contains(op)) {
			webResult.msg = "op error";
			return;
		}
		if (StringUtils.isBlank(content)) {
			webResult.msg = "content blank";
			return;
		}
		
		
		webResult.op = op;
		
		 if (OP_BIT.equals(op)) {
			 byte[] bytes = content.getBytes(charset);
	            StringBuilder sb = new StringBuilder();
	            for(byte b:bytes) {
	            	sb.append(byteToBit(b)).append(" ");
	            }
				webResult.msg = sb.toString();
				return;
		 }

	    if (OP_BYTE.equals(op)) {

			
            byte[] bytes = content.getBytes(charset);
            StringBuilder sb = new StringBuilder();
            for(byte b:bytes) {
            	sb.append(b);
            }
			webResult.msg = sb.toString();

			return;
		}
		if (OP_MD5.equals(op)) {

			
            byte[] bytes = content.getBytes(charset);
			webResult.msg = DigestUtils.md5Hex(bytes);

			return;
		}

		if (OP_SHA256.equals(op)) {
			 byte[] bytes = content.getBytes(charset);
		     webResult.msg = DigestUtils.sha256Hex(bytes);
			return;
		}
		
		if (OP_SHA384.equals(op)) {
			 byte[] bytes = content.getBytes(charset);
		     webResult.msg = DigestUtils.sha384Hex(bytes);
			return;
		}
		
		if (OP_SHA512.equals(op)) {
			 byte[] bytes = content.getBytes(charset);
		     webResult.msg = DigestUtils.sha512Hex(bytes);
			return;
		}
		
		if (OP_URLENCODE.equals(op)) {
			 
			
			
		    webResult.msg = URLEncoder.encode(content, charset);
			return;
		}

		
		if (OP_URLDECODE.equals(op)) {
		     webResult.msg = URLDecoder.decode(content, charset);
			return;
		}
		
		

		if (OP_BASE64ENCODE.equals(op)) {
			 String str = Base64.getEncoder().encodeToString(content.getBytes(charset));
		     webResult.msg = str;
			return;
		}
		

		if (OP_BASE64DECODE.equals(op)) {
			 byte[] bytes = Base64.getDecoder().decode(content);
		     webResult.msg = new String(bytes,charset);
			return;
		}


		if (OP_BASE64URLENCODE.equals(op)) {
			 String str = Base64.getUrlEncoder().encodeToString(content.getBytes(charset));
		     webResult.msg = str;
			return;
		}
		

		if (OP_BASE64URLDECODE.equals(op)) {
			 byte[] bytes = Base64.getUrlDecoder().decode(content);
		     webResult.msg = new String(bytes,charset);
			return;
		}
		
		if (OP_unicodeencode.equals(op)) {
			 
		     webResult.msg = UnicodeUtil.encode(content);
			return;
		}
		
		if (OP_unicodedecode.equals(op)) {
		     webResult.msg = UnicodeUtil.decode(content);
			return;
		}
		
		
		

		
		throw new Exception("op error,"+op);


	}
	
	

}
